package com.hospital.controller.admin;


import com.hospital.dao.WardDao;
import com.hospital.dao.impl.WardDaoImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
/**
 * 处理管理员添加新病床的请求，验证输入并更新数据库
 */
@WebServlet("/admin/addBed")
public class AddBedServlet extends HttpServlet {
    /** 病房数据访问对象，用于处理病房和床位信息的数据库操作 */
    private WardDao wardDao = new WardDaoImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        
        resp.setContentType("application/json;charset=UTF-8");
        
        try {
            int wardId = Integer.parseInt(req.getParameter("wardId"));
            boolean success = wardDao.addBed(wardId);
            
            if (success) {
                resp.getWriter().write("{\"message\": \"病床添加成功\"}");
            } else {
                resp.setStatus(500);
                resp.getWriter().write("{\"error\": \"病床添加失败\"}");
            }
            
        } catch (SQLException e) {
            resp.setStatus(500);
            resp.getWriter().write("{\"error\": \"数据库错误：" + e.getMessage() + "\"}");
            e.printStackTrace();
        } catch (NumberFormatException e) {
            resp.setStatus(400);
            resp.getWriter().write("{\"error\": \"无效的病房ID\"}");
        }
    }
} 